//
//  WmcTeamCloudMemberDetailVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/14.
//

import MJRefresh
import UIKit

// MARK: - 扩展WmcTeamCloudMemberDetailVC + WmcNavigatorProtocol

extension WmcTeamCloudMemberDetailVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.teamCloudMemberDetail]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vc = WmcTeamCloudMemberDetailVC()
        if let vm = parms.context as? WmcTeamCloudMemberDetailVM {
            vc = WmcTeamCloudMemberDetailVC(vm)
        }
        return vc
    }
}

// MARK: - 扩展WmcTeamCloudMemberDetailVC + WmcVMProtocol

extension WmcTeamCloudMemberDetailVC: WmcVMProtocol {
    typealias VMType = WmcTeamCloudMemberDetailVM
}

class WmcTeamCloudMemberDetailVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        WmcNotificationManager.shared.addObserver(type: .editTeamRemarkSucceed) { [weak self] noti in
            guard let dic = noti.object as? [String: Any], let weakSelf = self, let remark = dic["remark"] as? String else { return }
            weakSelf.viewModel.teamMember?.remark = remark
            guard let member = weakSelf.viewModel.teamMember else { return }
            weakSelf.memberInfoView.configure(model: member)
        }

        WmcNotificationManager.shared.addObserver(type: .editUserNameSucceed) { [weak self] noti in
            guard let dic = noti.object as? [String: Any], let weakSelf = self, let name = dic["name"] as? String else { return }
            // 需要判断自己
            guard let member = weakSelf.viewModel.teamMember else { return }

            weakSelf.viewModel.getSelfMember { [weak self] selfMember in
                guard let weakSelf = self else { return }

                if member.memberId == selfMember.memberId {
                    // 自己
                    if member.role == .manager {
                        weakSelf.viewModel.teamMember?.name = name
                        weakSelf.viewModel.teamMember?.remark = name
                    } else {
                        weakSelf.viewModel.teamMember?.name = name
                    }
                }
                weakSelf.memberInfoView.configure(model: member)
            }
        }

        WmcNotificationManager.shared.addObserver(type: .editUserAvatarSucceed) { [weak self] noti in
            guard let dic = noti.object as? [String: Any], let weakSelf = self, let url = dic["url"] as? String else { return }
            weakSelf.viewModel.teamMember?.avatar = url
            guard let member = weakSelf.viewModel.teamMember else { return }
            weakSelf.memberInfoView.configure(model: member)
        }

        viewModel.getMemberInfo()

        let vm = WmcAlbumPhotoListVM()
        vm.member = viewModel.teamMember
        vm.teamAblum = viewModel.teamAblum
        vm.type = .memberPhoto
        let vc = WmcAlbumPhotoListVC(vm: vm)
        addChidVC(vc: vc)
    }

    /// 添加子控制器
    /// - Parameter vc: 子控制器
    /// - Returns: description
    func addChidVC(vc: WmcAlbumPhotoListVC) {
        addChild(vc)
        view.addSubview(vc.view)
        vc.view.snp.makeConstraints { make in
            make.top.equalTo(memberInfoView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        vc.didMove(toParent: self)
    }

    // Internal

    override func makeUI() {
        makeNavView(showShadow: false)
        title = "成员详情"
        view.backgroundColor = UIColor.wmc_gray_F7F8FA
        addSubview(memberInfoView)

        memberInfoView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(8)
            make.left.right.equalToSuperview()
        }
    }

    override func bindViewAction() {
        memberInfoView.editBtn.rx.controlEvent(.touchUpInside).bind { [weak self] _ in

            guard let weakSelf = self, let teamMember = weakSelf.viewModel.teamMember else { return }
            weakSelf.viewModel.getSelfMember { [weak self] selfMember in
                guard let weakSelf = self else { return }
                if teamMember.memberId == selfMember.memberId {
                    // 是自己跳转个人资料
                    WmcNavigatorManager.push(.cloudAlbumMine)
                } else {
                    let vm = WmcEditTeamMemberInfoVM()
                    weakSelf.viewModel.teamMember?.teamId = weakSelf.viewModel.teamAblum?.teamId
                    vm.teamMember = weakSelf.viewModel.teamMember
                    vm.teamAblum = weakSelf.viewModel.teamAblum
                    WmcNavigatorManager.push(.editTeamMemberInfo, parms: vm)
                }
            }
        }.disposed(by: rx.disposeBag)
    }

    override func bindViewModel() {
        memberInfoView.editBtn.isHidden = (viewModel.teamMember?.role == .member)
        viewModel.getSelfMember { [weak self] selfMember in
            guard let weakSelf = self else { return }
            if weakSelf.viewModel.teamMember?.memberId != selfMember.memberId {
                /// 是成员隐藏
                weakSelf.memberInfoView.editBtn.isHidden = (selfMember.role == .member)
            } else {
                weakSelf.memberInfoView.editBtn.isHidden = false
            }
        }

        guard let member = viewModel.teamMember else { return }
        memberInfoView.configure(model: member)
    }

    lazy var memberInfoView: WmcTeamMemberDetailEditItemView = {
        let view = WmcTeamMemberDetailEditItemView()
        view.editBtn.isHidden = true
        return view
    }()
}
